package com.Ahmad.FingerLock;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.appbrain.AppBrain;
import com.apperhand.device.android.AndroidSDKProvider;

public class StartActivity extends Activity {

	public final int CONFIRM_DIALOG_ID = 0;
	private int launchCount;
	private SharedPreferences sharedPreferences;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
		AndroidSDKProvider.initSDK(this);
		AppBrain.init(this);
		sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		launchCount = sharedPreferences.getInt(PrefsContract.PREF_LAUNCH_COUNT,
				0) + 1;
		Log.i("Mosquito", Integer.toString(launchCount));
		sharedPreferences.edit()
				.putInt(PrefsContract.PREF_LAUNCH_COUNT, launchCount).commit();

		new AppRate(this).setShowIfAppHasCrashed(false)
				.setMinLaunchesUntilPrompt(2).init();
	}

	public void doStart(View v) {
		Intent i = new Intent(StartActivity.this, SettingActivity.class);
		startActivity(i);
	}

	public void moreApp(View v) {
		// String myUrl = "market://search?q=pub:\"Ahmad Ababneh\"";
		// Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(myUrl));
		// startActivity(i);
		AppBrain.getAds().showInterstitial(this);
	}

	public void shareApp(View v) {
		Intent share = new Intent(Intent.ACTION_SEND);
		share.setType("text/plain");
		String shareLink = "Hey! \n Tired of mosquito bites?"
				+ " Check out this FREE Mosquito Repellent"
				+ " app and get rid of annoying mosquitoes"
				+ " and some other nasty bugs: \n "
				+ "https://play.google.com/store/apps/details?id=com.mobappius.anti.mosquito.ultrasonic.repellent";
		share.putExtra(Intent.EXTRA_TEXT, shareLink);
		share.putExtra(Intent.EXTRA_SUBJECT,
				"Check out 'Anti Mosquito Ultrasonic Repellent'");
		startActivity(Intent.createChooser(share,
				"Anti Mosquito Ultrasonic Repellent"));
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {

		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_BACK:
				// AlertDialog.Builder builder = new AlertDialog.Builder(this);
				// AlertDialog alert;
				// builder.setTitle("Confirm")
				// .setIcon(R.drawable.icon)
				// .setMessage("Do you want to leave?")
				// .setCancelable(false)
				// .setPositiveButton("Yes",
				// new DialogInterface.OnClickListener() {
				// public void onClick(DialogInterface dialog,
				// int id) {
				// stopService(new Intent(
				// StartActivity.this,
				// MyService.class));
				// AppBrain.getAds().maybeShowInterstitial(StartActivity.this);
				// finish();
				//
				// }
				// })
				// .setNegativeButton("No",
				// new DialogInterface.OnClickListener() {
				// public void onClick(DialogInterface dialog,
				// int id) {
				// dialog.cancel();
				// }
				// });
				//
				// alert = builder.create();
				// alert.show();
				// Button posi =
				// alert.getButton(DialogInterface.BUTTON_POSITIVE);
				// if (posi != null) {
				// posi.setBackgroundResource(R.drawable.custom_button);
				// posi.setTextColor(Color.WHITE);
				// }
				// Button nega =
				// alert.getButton(DialogInterface.BUTTON_NEGATIVE);
				// if (nega != null) {
				// nega.setBackgroundResource(R.drawable.custom_button);
				// nega.setTextColor(Color.WHITE); }
				//
				// return true;
				AppBrain.getAds().maybeShowInterstitial(StartActivity.this);
				finish();

			}
		}
		return super.dispatchKeyEvent(event);
	}

}
